// subpackagesA/pages/ThermometricMonitoring/ThermometricMonitoring.ts
import dayjs from '../../../utils/day'
import { findAllInfo, MetervillageServer } from '../../../api/room-monitoring'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTime: false,
    showVillage: false,
    columns: [],
    minDate: new Date(dayjs().subtract(1, "month")).getTime(),
    maxDate: new Date(dayjs()).getTime(),
    time: "",
    village: "",
    villageID: 0,
    data: [],
    isFresh: false,
    page: 1,
    limit: 5,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getData();
  },
  getData() {
    const that = this;
    that.setData({
      isFresh: true,
      page: 1
    })
    const comID = wx.getStorageSync("userInfo").data.comID;
    const villageID = that.data.villageID;
    const page = that.data.page;
    const limit = that.data.limit;
    const time = that.data.time;
    findAllInfo(comID, time, villageID, page, limit).then((res: any) => {
      that.setData({
        data: res.data.data,
        isFresh: false
      })
    })
  },
  fresh() {
    this.getData();
  },
  onClickLeft() {
    wx.navigateBack({ delta: -1 });
  },
  onTime() {
    this.setData({ showTime: true });
  },
  onClose() {
    this.setData({ showTime: false, showVillage: false });
  },
  onCalender(e:any) {
    const time = dayjs(e.detail).format("YYYY-MM-DD");
    const villageID = this.data.villageID;
    this.setData({
      showTime:false,
      time:time
    });
    if(villageID==0||time==""){
      wx.showToast({ title: "选择时间小区", icon: "error" })
    }else{
      this.getData();
    }
  },
  onVillage() {
    this.setData({ showVillage: true });
  },
  onChange(e: any) {
    const villageID = e.detail.value.villageID;
    const time = this.data.time;
    this.setData({
      showVillage: false,
      village: e.detail.value.villageName,
      villageID: villageID
    })
    if (villageID == 0 || time == '') {
      wx.showToast({ title: "选择时间小区", icon: "error" })
    } else {
      this.getData();
    }
  },
  onPage() {
    const that = this;
    that.setData({
      page: that.data.page + 1
    });
    const comID = wx.getStorageSync("userInfo").data.comID;
    const villageID = that.data.villageID;
    const page = that.data.page;
    const limit = that.data.limit;
    const time = that.data.time;
    findAllInfo(comID, time, villageID, page, limit).then((res: any) => {
      that.setData({
        data: [...that.data.data, ...res.data.data],
        isFresh: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const comID = wx.getStorageSync("userInfo").data.comID
    MetervillageServer(comID).then((res: any) => {
      this.setData({ columns: res.data });
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})